<?php
namespace app\admin\controller;
use app\admin\model\Customers;
use app\admin\model\Users;
use app\admin\controller\AdminAuth;
use think\Validate;
use think\Image;
use think\Db;
use think\Request;
class AllfollowController extends AdminAuth
{
	//模块基本信息
	private $data = array(
		'module_name' => '所有跟进',
		'module_url'  => '/admin/Allfollow/',
		'module_slug' => 'Customers',
		'upload_path' => UPLOAD_PATH,
		'upload_url'  => '/public/uploads/',
    );

    /**
     * @return string|\think\response\View 客户跟进记录
     */
    public function index(){
        $where = [];
        input('user_id') ? $where['D.user_id'] = input('user_id') : false; //跟进者id
        if(input('startTime') && input('endTime')){
            $start = strtotime(input('startTime'));
            $end = strtotime(input('endTime'));
            $where['F.follow_time'] = ['between',[$start, $end]];
        }
        $followloglist = Db::name('follow_up')
            ->alias('F')
            ->join('distribution D','F.customers_id = D.customers_id')
            ->join('users U','D.user_id = U.id')
            ->field('F.*,U.nickname')
            ->where($where)
            ->order('follow_id desc')
            ->paginate(10);

        $this->assign('followloglist',$followloglist);
        $allusers = Db::name('users')->field('id,nickname')->order('id ASC')->select();
        $this->assign('allusers',$allusers);
        $this->assign('data',$this->data);
        return view();
    }
    //删除跟进log

    /**
     * @return mixed 跟进删除
     */
    public function delfollow(){
        $follow_id = input('follow_id');
        $data['follow_id'] = input('follow_id');
        if ($follow_id) {
            $delfollow = Db::name('follow_up')->where(['follow_id'=>$follow_id])->delete();
            $data['error'] = 0;
            $data['msg'] = '删除成功';
        } else {
            $data['error'] = 1;
            $data['msg'] = '删除失败';
        }
        return $data;
    }

    /**
     * @return \think\response\Json 跟进记录详情
     */
    public function followdetail(){
        $follow_id = input('follow_id');
        $re_follow = Db::name('follow_up')->where(['follow_id'=>$follow_id])->find();
        $re_follow['follow_time'] = date('Y-m-d H:i',$re_follow['follow_time']);
        $re_follow['next_follow_time'] = date('Y-m-d H:i',$re_follow['next_follow_time']);
        return json($re_follow);
    }

    /**
     * /更新跟進记录记录
     */
    public function updatefollowlog(){
        $data = input('post.');
        $data['follow_time'] = strtotime($data['follow_time']);
        $data['next_follow_time'] = strtotime($data['next_follow_time']);
        $re = Db::name('follow_up')->where(['follow_id'=>$data['follow_id']])->update($data);
        if($re){
            return $this->success('更新成功',$this->data['module_url'],'','1');
        }else{
            return $this->error('更新上失败',$this->data['module_url'],'','1');
        }
    }
}